package com.yxjx.duoxue.customview;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.utils.UIHelper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class YxRadioGroup extends LinearLayout {

	public YxRadioGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		View root = LayoutInflater.from(context).inflate(R.layout.segment_yx_radio_group, this, true);
		
		child1 = root.findViewById(R.id.select_1);
		child2 = root.findViewById(R.id.select_2);
		child1.setOnClickListener(clickListener);
		child2.setOnClickListener(clickListener);
		
	}
	
	/**
	 * 显示先生女士。
	 */
	public void customizeLabels() {
		UIHelper.setText(child1, R.id.text, "先生");
		UIHelper.setText(child2, R.id.text, "女士");
	}
	
	private View.OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.select_1:
				setSelectedIndex(1);
				break;
			case R.id.select_2:
				setSelectedIndex(2);
				break;
			default:
				break;
			}
			
		}
	};
	
	// 性别1-男2-女，其他值－未选中
	private int selected = -1;
	private View child1;
	private View child2;
	
	public int getSelectedIndex() {
		return selected;
	}
	
	public void setSelectedIndex(int selected) {
		this.selected = selected;
		update();
	}
	
	private void update() {
		if (this.selected == 1) {
			child1.setSelected(true);
			child2.setSelected(false);
		} else if (this.selected == 2) {
			child1.setSelected(false);
			child2.setSelected(true);
		} else {
			child1.setSelected(false);
			child2.setSelected(false);
		}
	}
	

}
